package c.j.a.d.b.j;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build.VERSION;
import android.os.Handler;
import android.os.IBinder;
import android.os.IBinder.DeathRecipient;
import android.os.Looper;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;
import android.util.SparseArray;
import c.j.a.d.a.i;
import c.j.a.d.b.b.e;
import c.j.a.d.b.d.g;
import c.j.a.d.b.e.d;
import c.j.a.d.b.e.m;
import c.j.a.d.b.e.o;
import c.j.a.d.b.e.p;
import c.j.a.d.b.e.q;
import c.j.a.d.b.n.c0;
import com.ss.android.socialbase.downloader.downloader.IndependentProcessDownloadService;
import com.ss.android.socialbase.downloader.g.c;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.json.JSONException;
import org.json.JSONObject;

/* compiled from: IndependentDownloadServiceHandler */
public class s extends c.j.a.d.b.e.a implements ServiceConnection {
    public static final String o = s.class.getSimpleName();
    public static int p = 0;
    public static long q = 0;
    public m i;
    public q j;
    public int k = -1;
    public Handler l = new Handler(Looper.getMainLooper());
    public boolean m;
    public ServiceConnection n;

    /* compiled from: IndependentDownloadServiceHandler */
    public class a implements DeathRecipient {

        /* compiled from: IndependentDownloadServiceHandler */
        public class a implements Runnable {
            public void run() {
                String str = s.o;
                c.j.a.d.b.g.a.d(s.o, "run: restart downloader process !!");
                s sVar = s.this;
                sVar.m = true;
                try {
                    sVar.e(c.j.a.d.b.e.b.a(), s.this);
                } catch (Throwable th) {
                    th.printStackTrace();
                }
            }
        }

        public void binderDied() {
            String str = s.o;
            str = s.o;
            StringBuilder g = c.b.a.a.a.g("binderDied: mServiceConnection = ");
            g.append(s.this.n);
            c.j.a.d.b.g.a.f(str, g.toString());
            if (s.p < 5 && System.currentTimeMillis() - s.q > 15000) {
                s.this.l.postDelayed(new a(), 1000);
                s.p++;
                s.q = System.currentTimeMillis();
            }
        }
    }

    /* compiled from: IndependentDownloadServiceHandler */
    public class b implements Runnable {

        /* compiled from: IndependentDownloadServiceHandler */
        public class a implements Runnable {
            public void run() {
                try {
                    s.h(s.this);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        public void run() {
            c.j.a.d.b.e.b.q().execute(new a());
        }
    }

    public static void h(s sVar) {
        Objects.requireNonNull(sVar);
        String str = o;
        c.j.a.d.b.g.a.d(str, "resumeDownloaderProcessTaskForDied: ");
        if (c.j.a.d.b.e.b.a() != null && !TextUtils.isEmpty(e.a)) {
            o w = c.j.a.d.b.e.b.w();
            if (w != null) {
                p a = q.a(true);
                if (a != null) {
                    List<c> j = a.j(e.a);
                    if (j != null && !j.isEmpty()) {
                        ArrayList arrayList = new ArrayList();
                        for (c cVar : j) {
                            if (cVar != null && cVar.K && cVar.p0() == -5) {
                                arrayList.add(cVar);
                            }
                        }
                        if (!arrayList.isEmpty()) {
                            StringBuilder g = c.b.a.a.a.g("resumeDownloaderProcessTaskForDied: resume size =");
                            g.append(arrayList.size());
                            c.j.a.d.b.g.a.d(str, g.toString());
                            ((i.c) w).c(arrayList, 1);
                        }
                    }
                }
            }
        }
    }

    public IBinder a(Intent intent) {
        String str = o;
        if (intent != null && intent.getBooleanExtra("fix_downloader_db_sigbus", false)) {
            Log.w(str, "downloader process sync database on main process!");
            String str2 = "fix_sigbus_downloader_db";
            c0 c0Var = c.j.a.d.b.l.a.e;
            try {
                if (c.j.a.d.b.l.a.h == null) {
                    c.j.a.d.b.l.a.h = new JSONObject();
                }
                c.j.a.d.b.l.a.h.put(str2, 1);
            } catch (JSONException unused) {
            }
        }
        c.j.a.d.b.g.a.d(str, "onBind IndependentDownloadBinder");
        return new r();
    }

    public void b(c.j.a.d.b.h.c cVar) {
        if (cVar != null) {
            d.b().d(cVar.m(), true);
            c B = c.j.a.d.b.e.b.B();
            if (B != null) {
                B.g(cVar);
            }
        }
    }

    public void c(q qVar) {
        this.j = qVar;
    }

    public void e(Context context, ServiceConnection serviceConnection) {
        try {
            c.j.a.d.b.g.a.d(o, "bindService");
            Intent intent = new Intent(context, IndependentProcessDownloadService.class);
            if (c.j.a.d.b.n.a.r()) {
                intent.putExtra("fix_downloader_db_sigbus", c.j.a.d.b.l.a.f.k("fix_sigbus_downloader_db", false));
            }
            if (serviceConnection != null) {
                context.bindService(intent, serviceConnection, 1);
            }
            this.n = serviceConnection;
            context.startService(intent);
        } catch (Throwable th) {
            th.printStackTrace();
        }
    }

    public void f() {
        if (this.i == null) {
            e(c.j.a.d.b.e.b.a(), this);
        }
    }

    public void onBindingDied(ComponentName componentName) {
        this.i = null;
        q qVar = this.j;
        if (qVar != null) {
            ((u) qVar).a = null;
        }
    }

    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        String str = o;
        c.j.a.d.b.g.a.d(str, "onServiceConnected ");
        this.i = c.j.a.d.b.e.m.a.y0(iBinder);
        c.j.a.d.b.e.b.a();
        int i = 0;
        if (VERSION.SDK_INT < 26 && c.j.a.c.y.a.i.s(512) && c.j.a.d.b.n.a.r()) {
            try {
                iBinder.linkToDeath(new a(), 0);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
            if (this.m) {
                this.l.postDelayed(new b(), 1000);
                this.m = false;
            }
        }
        q qVar = this.j;
        if (qVar != null) {
            u uVar = (u) qVar;
            Objects.requireNonNull(uVar);
            uVar.a = c.j.a.d.b.e.m.a.y0(iBinder);
            if (c.j.a.d.b.n.a.r()) {
                uVar.u(new t(uVar));
            }
        }
        StringBuilder g = c.b.a.a.a.g("onServiceConnected aidlService!=null");
        g.append(this.i != null);
        g.append(" pendingTasks.size:");
        g.append(this.b.size());
        c.j.a.d.b.g.a.d(str, g.toString());
        if (this.i != null) {
            d b = d.b();
            synchronized (b.c) {
                for (g gVar : b.c) {
                    if (gVar != null) {
                        gVar.a();
                    }
                }
            }
            this.c = true;
            this.e = false;
            int i2 = this.k;
            if (i2 != -1) {
                try {
                    this.i.G(i2);
                } catch (RemoteException e2) {
                    e2.printStackTrace();
                }
            }
            synchronized (this.b) {
                if (this.i != null) {
                    SparseArray clone = this.b.clone();
                    this.b.clear();
                    while (i < clone.size()) {
                        c.j.a.d.b.h.c cVar = (c.j.a.d.b.h.c) clone.get(clone.keyAt(i));
                        if (cVar != null) {
                            try {
                                this.i.I0(c.j.a.d.b.n.d.e(cVar));
                            } catch (RemoteException e3) {
                                e3.printStackTrace();
                            }
                        }
                        i++;
                    }
                }
            }
        }
    }

    public void onServiceDisconnected(ComponentName componentName) {
        c.j.a.d.b.g.a.d(o, "onServiceDisconnected ");
        this.i = null;
        this.c = false;
        q qVar = this.j;
        if (qVar != null) {
            ((u) qVar).a = null;
        }
    }

    public void a(c.j.a.d.b.h.c cVar) {
        if (cVar != null) {
            String str = o;
            StringBuilder g = c.b.a.a.a.g("tryDownload aidlService == null:");
            g.append(this.i == null);
            c.j.a.d.b.g.a.d(str, g.toString());
            if (this.i == null) {
                f(cVar);
                e(c.j.a.d.b.e.b.a(), this);
            } else {
                if (this.b.get(cVar.m()) != null) {
                    synchronized (this.b) {
                        if (this.b.get(cVar.m()) != null) {
                            this.b.remove(cVar.m());
                        }
                    }
                }
                try {
                    this.i.I0(c.j.a.d.b.n.d.e(cVar));
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                synchronized (this.b) {
                    SparseArray clone = this.b.clone();
                    this.b.clear();
                    if (c.j.a.d.b.e.b.B() != null) {
                        for (int i = 0; i < clone.size(); i++) {
                            if (((c.j.a.d.b.h.c) clone.get(clone.keyAt(i))) != null) {
                                try {
                                    this.i.I0(c.j.a.d.b.n.d.e(cVar));
                                } catch (RemoteException e2) {
                                    e2.printStackTrace();
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    public void a(int i) {
        m mVar = this.i;
        if (mVar == null) {
            this.k = i;
            return;
        }
        try {
            mVar.G(i);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}
